package com.nm.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nm.commom.Result;
import com.nm.mapper.ExpendMapper;
import com.nm.mapper.StopMapper;
import com.nm.pojo.Expend;
import com.nm.pojo.Stop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 *支付接口
 */
//@CrossOrigin
@RestController
@RequestMapping(value = "/expend")
public class ExpendController {

    @Autowired
    private ExpendMapper expendMapper;

    @Autowired
    private StopMapper stopMapper;

    /**
     *查询全部的支付记录
     */
    @RequestMapping(value = "/query")
    public Result query(){
        List<Map<Object, Object>> maps = new ArrayList<>();
        List<Expend> expends = expendMapper.selectList(null);
        for (Expend expend : expends) {
            Stop stop = stopMapper.selectById(expend.getStopId());
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("expendId",expend.getExpendId());
            objectObjectHashMap.put("totalMoney",expend.getTotalMoney());
            objectObjectHashMap.put("userMoney",expend.getUserMoney());
            objectObjectHashMap.put("ptMoney",expend.getPtMoney());
            objectObjectHashMap.put("expendTime",expend.getExpendTime());
            objectObjectHashMap.put("nickName",expend.getNickName());
            objectObjectHashMap.put("scoreStatus",stop.getScoreStatus());
            objectObjectHashMap.put("score",stop.getScore());
            objectObjectHashMap.put("useStatus",stop.getUseStatus());
            maps.add(objectObjectHashMap);
        }
        return Result.success(maps);
    }
    /**
     *查询当天的支付记录
     */
    //
    @RequestMapping(value = "/queryToday")
    public Result querytoday(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置日期格式
        Date nowDate = new Date();
        String nowTime = df.format(nowDate);
        Calendar now = Calendar.getInstance();
        now.setTime(nowDate);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - 1);
        Date preDate = now.getTime();
        String preTime = df.format(preDate); //前一天
        now.setTime(nowDate);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + 1);
        Date preDate1 = now.getTime();
        String preTime1 = df.format(preDate1); //后一天
        QueryWrapper<Expend> wrapper = new QueryWrapper<>();
        wrapper.between("expend_time",preTime,preTime1);
        List<Expend> list = expendMapper.selectList(wrapper);
        return Result.success(list);
    }

    /**
     *根据订单编号查询支付信息
     * @param expendId 订单编号
     */
    @GetMapping("/queryById")
    public Result name(@RequestParam int expendId){
        Expend expend = expendMapper.selectById(expendId);
        Stop stop = stopMapper.selectById(expend.getStopId());
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("expendId",expend.getExpendId());
        objectObjectHashMap.put("totalMoney",expend.getTotalMoney());
        objectObjectHashMap.put("userMoney",expend.getUserMoney());
        objectObjectHashMap.put("ptMoney",expend.getPtMoney());
        objectObjectHashMap.put("expendTime",expend.getExpendTime());
        objectObjectHashMap.put("nickName",expend.getNickName());
        objectObjectHashMap.put("scoreStatus",stop.getScoreStatus());
        objectObjectHashMap.put("score",stop.getScore());
        objectObjectHashMap.put("useStatus",stop.getUseStatus());
        objectObjectHashMap.put("stallId",stop.getStallId());
        objectObjectHashMap.put("startTime",stop.getStartTime());
        objectObjectHashMap.put("endTime",stop.getOverTime());
        objectObjectHashMap.put("stopPlace",stop.getStopPlace());
        return Result.success(objectObjectHashMap);
    }
}
